package com.test.demos.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.test.demos.pojo.dto.MedicalInstitutionsDTO;
import com.test.demos.pojo.entity.MedicalInstitutions;
import com.test.demos.mapper.MedicalInstitutionsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.test.demos.service.MedicalInstitutionsService;

import java.util.List;

import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * (MedicalInstitutions)表服务接口
 *
 * @author twt
 * @since 2025-04-14 16:26:45
 */
@Service
@Transactional
public class MedicalInstitutionsServiceImpl implements MedicalInstitutionsService {

    @Autowired
    private MedicalInstitutionsMapper medicalInstitutionsMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param institutionId 主键
     * @return 实例对象
     */
    @Override
    public MedicalInstitutions queryById(Integer institutionId) {
        MedicalInstitutions select = medicalInstitutionsMapper.selectOneById(institutionId);
        if (isEmpty(select)) {
            throw new RuntimeException("未查询到对应数据");
        }
        return select;
    }

    /**
     * 插入数据
     *
     * @param dto
     */
    @Override
    public void add(MedicalInstitutionsDTO dto) {
        MedicalInstitutions medicalInstitutions = new MedicalInstitutions();
        BeanUtils.copyProperties(dto,medicalInstitutions);
        Integer pkValue = medicalInstitutions.getInstitutionId();

        if (medicalInstitutionsMapper.selectOneById(pkValue) != null) {
            throw new RuntimeException("数据已存在，无法插入");
        }
        int insert = medicalInstitutionsMapper.insert(medicalInstitutions);
        if (insert != 1) {
            throw new RuntimeException("插入数据失败");
        }
    }

    @Override
    public List<MedicalInstitutions> queryAll() {
        return medicalInstitutionsMapper.selectAll();
    }

    @Override
    public List<MedicalInstitutions> getAllPackage(Integer institutionId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select(MedicalInstitutions::getInstitutionId,MedicalInstitutions::getInstitutionName);
        return medicalInstitutionsMapper.selectListByQuery(queryWrapper);
    }

    /**
     * 更新数据
     *
     * @param dto
     */
    @Override
    public void update(MedicalInstitutionsDTO dto) {
        MedicalInstitutions medicalInstitutions = new MedicalInstitutions();
        BeanUtils.copyProperties(dto,medicalInstitutions);
        int i = medicalInstitutionsMapper.update(medicalInstitutions);
        if (i != 1) {
            throw new RuntimeException("更新数据失败");
        }
    }

    /**
     * 根据主键删除数据
     *
     * @param institutionId
     */
    @Override
    public void deleteById(Integer institutionId) {
        int i = medicalInstitutionsMapper.deleteById(institutionId);
        if (i != 1) {
            throw new RuntimeException("删除数据失败");
        }
    }

}
